﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;
using System.IO;


namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Ajout d'un ou plusieurs fichiers
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Files")]
    public class Add_Files : Cmdlet
    {
        public Add_Files()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.Target = TFSProvider.s_root;
            }
            this.Description = string.Empty;
                this.Recursion = RecursionType.None;

        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
            }
            if (string.IsNullOrEmpty(Target))
            {
                WriteError(new ErrorRecord(new ArgumentException("Not target!!!"), "PsTFS", ErrorCategory.ObjectNotFound, Target));
            }
            try
            {
                //Création du répertoire temporaire pour le workspace
                string tmp = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\tempPsTFS\" + Guid.NewGuid().ToString();

                PsMethode.AddFiles(tmp, Target, Source, TFS, Description, Recursion);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

        }


        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Add-Files is end");
            base.EndProcessing();
        }

        

        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Source { get; set; }

        [ValidateNotNullOrEmpty]
        public string Target { get; set; }

        [ValidateNotNullOrEmpty]
        public string Description { get; set; }

        [ValidateNotNullOrEmpty]
        public RecursionType Recursion { get; set; }

        [Parameter(Position = 5)]
        [ValidateNotNullOrEmpty]
        public bool Detail { get; set; }
    }
}


